import { save, remove, clear } from "@/assets/js/array-store";
import { SEARCH_KEY } from "@/assets/js/constant";
import { useStore } from "vuex";

export default function useSearchHistory() {
  const maxLength = 100;
  const store = useStore();

  function saveSearch(query) {
    const searches = save(
      query,
      SEARCH_KEY,
      (item) => {
        return item === query;
      },
      maxLength
    );
    store.commit("setSearchHistory", searches);
  }

  function deleteSearch(query) {
    const searches = remove(SEARCH_KEY, (item) => item === query);
    store.commit("setSearchHistory", searches);
  }

  function clearSearch() {
    const searches = clear(SEARCH_KEY);
    store.commit("setSearchHistory", searches);
  }

  return {
    saveSearch,
    deleteSearch,
    clearSearch,
  };
}
